/**
 * 
 */
package com.gr.staffpm.pages.admin.role;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.markup.html.form.palette.Palette;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.util.CollectionModel;
import org.apache.wicket.model.util.ListModel;
import org.joda.time.Instant;

import com.gr.staffpm.datatypes.Permission;
import com.gr.staffpm.datatypes.Role;
import com.gr.staffpm.datatypes.User;
import com.gr.staffpm.pages.behaviour.FocusOnLoadBehavior;
import com.gr.staffpm.security.service.UserService;


/**
 * @author Graham Rhodes 2 May 2011 21:25:08
 */
public class EditRoleForm extends Form<Role> {

    private static final long serialVersionUID = 1L;
    private final Palette<Permission> permissionsPalette;
    private final TextField<String> role;
    private final TextField<Integer> level;

    public EditRoleForm(String id, final UserService userService, final Role r) {
        super(id, new CompoundPropertyModel<Role>(r));

        role = new TextField<String>("role");
        role.add(new FocusOnLoadBehavior());
        role.setRequired(true);
        add(role);

        level = new TextField<Integer>("level");
        level.setRequired(true);
        add(level);

        List<Permission> permissions = userService.getAllPermissions();
        IChoiceRenderer<Permission> permissionRenderer = new ChoiceRenderer<Permission>("permission", "id");
        permissionsPalette =
                new Palette<Permission>("permissions", new ListModel<Permission>(new ArrayList<Permission>()), new CollectionModel<Permission>(permissions), permissionRenderer, 10, false);
        permissionsPalette.setDefaultModelObject(r.getPermissions());
        add(permissionsPalette);

        add(new AjaxButton("saveButton") {

            private static final long serialVersionUID = 1L;

            @SuppressWarnings("unchecked")
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                User currUser = userService.getCurrentUser();

                r.setRole(role.getValue());
                r.setLevel(level.getConvertedInput());
                r.setLastUpdated(new Instant().toDate());
                r.setUpdatedBy(currUser);
                r.getPermissions().addAll((Set<Permission>) permissionsPalette.getDefaultModelObject());

                userService.createOrUpdateRole(r);

                //Send user back to previous page
                setResponsePage(new AdminRolesPage());
            }

        });

        AjaxButton cancel = new AjaxButton("cancelButton") {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                setResponsePage(AdminRolesPage.class);
            }

        };
        cancel.setDefaultFormProcessing(false);
        add(cancel);
    }

}
